package com.example.service.impl;

import com.example.mapper.AdminMapper;
import com.example.pojo.*;
import com.example.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
//    @Override
//    public List<Teacher> getTeacher() {
//        return adminMapper.getTeacher();
//    }


    @Override
    public UserPageBean selectAll(Integer page, Integer size, String name, String username, Integer gradeClassId, String object) {
        Integer count = adminMapper.getTeacherCount();
        Integer start = page * size;
        List<Teacher> list = adminMapper.getTeacherAll(start, size, name, username, gradeClassId, object);
        UserPageBean userPageBean = new UserPageBean(count, list, null, null);
        return userPageBean;
    }

    @Override
    public UserPageBean getParent(Integer page, Integer size, String name, String username, Integer gradeClassId, String studentName) {
        Integer count = adminMapper.getPatriarchCount();
        Integer start = page * size;
        List<Patriarch> list = adminMapper.getPatriarchAll(start, size, name, username, gradeClassId, studentName);
        UserPageBean userPageBean = new UserPageBean(count, null, list, null);
        return userPageBean;
    }

    @Override
    public void updateParent(Patriarch patriarch) {
        patriarch.setUpdateTime(LocalDateTime.now());
        adminMapper.updateParent(patriarch);
    }

    @Override
    public void updateTeacher(Teacher teacher) {
        teacher.setUpdateTime(LocalDateTime.now());
        adminMapper.updateTeacher(teacher);
    }

    @Override
    public UserPageBean getStudent(Integer page, Integer size, String name, Integer gender, Integer gradeClassId) {
        Integer count = adminMapper.getStudentCount();
        Integer start = page * size;
        List<Student> list = adminMapper.getStudentAll(start, size, name, gender, gradeClassId);
        UserPageBean userPageBean = new UserPageBean(count, null, null, list);
        return userPageBean;
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        adminMapper.updateStudent(student);
    }

    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        adminMapper.addStudent(student);
    }

    @Override
    public void addTeacher(Teacher teacher) {
        teacher.setCreateTime(LocalDateTime.now());
        teacher.setUpdateTime(LocalDateTime.now());
        adminMapper.addTeacher(teacher);
    }

    @Override
    public void addParent(Patriarch patriarch) {
        patriarch.setCreateTime(LocalDateTime.now());
        patriarch.setUpdateTime(LocalDateTime.now());
        adminMapper.addParent(patriarch);
    }


    @Override
    public WorkPageBean getAllPlan(Integer page, Integer size, String title, String content, String teacherName) {

        Integer count = adminMapper.seletAllPlan();

        Integer start = page * size;
        List<Plan> planList = adminMapper.getAllPlan(start, size, title, content, teacherName);
        WorkPageBean workPageBean = new WorkPageBean(count, planList, null, null, null);

        return workPageBean;
    }

    @Override
    public void updatePlan(Plan plan) {
        plan.setUpdateTime(LocalDateTime.now());
        adminMapper.updatePlan(plan);
    }

    @Override
    public void addPlan(Plan plan) {
        plan.setCreateTime(LocalDateTime.now());
        plan.setUpdateTime(LocalDateTime.now());
        plan.setStatus((short) 1);
        adminMapper.addPlan(plan);
    }

    @Override
    public void delPlan(Plan plan) {
        adminMapper.delPlan(plan.getId());
    }

    @Override
    public WorkPageBean getAllProgress(Integer page, Integer size, String title, String content, String teacherName) {
        Integer count = adminMapper.seletAllProgress();

        Integer start = page * size;
        List<Progress> progressList = adminMapper.getAllProgress(start, size, title, content, teacherName);
        WorkPageBean workPageBean = new WorkPageBean(count, null, progressList, null, null);

        return workPageBean;
    }

    @Override
    public void updateProgress(Progress progress) {
        progress.setUpdateTime(LocalDateTime.now());
        adminMapper.updateProgress(progress);
    }

    @Override
    public void addProgress(Progress progress) {
        progress.setCreateTime(LocalDateTime.now());
        progress.setUpdateTime(LocalDateTime.now());
        progress.setStatus((short) 1);
        adminMapper.addProgress(progress);
    }

    @Override
    public void delProgress(Progress progress) {
        adminMapper.delProgress(progress.getId());
    }

    @Override
    public WorkPageBean getAllHomeWork(Integer page, Integer size, String title, String content, String teacherName) {
        Integer count = adminMapper.seletAllHomeWork();

        Integer start = page * size;
        List<HomeWork> homeWorkList = adminMapper.getAllHomeWork(start, size, title, content, teacherName);
        WorkPageBean workPageBean = new WorkPageBean(count, null, null, homeWorkList, null);

        return workPageBean;
    }

    @Override
    public List<StudentHomeWork> seletDateHomeWork(Integer id) {
        return adminMapper.selectDateHomeWork(id);
    }

    @Override
    public void addHomeWork(HomeWork homeWork) {
        homeWork.setCreateTime(LocalDateTime.now());
        homeWork.setUpdateTime(LocalDateTime.now());
        adminMapper.addHomeWork(homeWork);
    }

    @Override
    public void delHomeWork(HomeWork homeWork) {
        adminMapper.delHomeWork(homeWork);
    }


    @Override
    public WorkPageBean getAllTask(Integer page, Integer size, String title, String content, String teacherName) {
        Integer count = adminMapper.seletAllTask();

        Integer start = page * size;
        List<Task> taskList = adminMapper.getAllTask(start, size, title, content, teacherName);
        WorkPageBean workPageBean = new WorkPageBean(count, null, null, null, taskList);

        return workPageBean;
    }

    @Override
    public List<StudentTask> selectDateTask(Integer id) {
        return adminMapper.selectDateTask(id);
    }

    @Override
    public void addTask(Task task) {
        task.setCreateTime(LocalDateTime.now());
        task.setUpdateTime(LocalDateTime.now());
        adminMapper.addTask(task);
    }

    @Override
    public void delTask(Task task) {
        adminMapper.delTask(task);
    }

    @Override
    public void addAnnoun(Announcemen announcemen) {
        announcemen.setRekedateTime(LocalDateTime.now());
        announcemen.setCreateTime(LocalDateTime.now());
        announcemen.setUpdateTime(LocalDateTime.now());
        adminMapper.addAnnoun(announcemen);
    }

    @Override
    public List<Announcemen> getAllAnnoun() {
        LocalDateTime localDateTime = LocalDateTime.now();
        List<Announcemen> announcemenList = adminMapper.getAllAnnoun(localDateTime);
        return announcemenList;
    }

    @Override
    public Teacher getName(String username) {
        return adminMapper.getName(username);
    }

    @Override
    public void addTest(Test test) {
        test.setRekedateTime(LocalDateTime.now());
        test.setCreateTime(LocalDateTime.now());
        test.setUpdateTime(LocalDateTime.now());
        adminMapper.addTest(test);
    }

    @Override
    public List<Test> getTest() {
        LocalDateTime localDateTime = LocalDateTime.now();
        List<Test> testList = adminMapper.getAllTest(localDateTime);
        return testList;
    }

    @Override
    public IndexDataAll allData() {
        Integer hwCount = adminMapper.selHomework();
        Integer planCount = adminMapper.selPlan();
        Integer proCount = adminMapper.selProgress();
        Integer taskCount = adminMapper.selTask();
        Integer announCount = adminMapper.selAnnouncemen();
        IndexDataA indexDataA1 = new IndexDataA("课后作业", hwCount);
        IndexDataA indexDataA2 = new IndexDataA("教学计划", planCount);
        IndexDataA indexDataA3 = new IndexDataA("教学进度", proCount);
        IndexDataA indexDataA4 = new IndexDataA("打卡任务", taskCount);
        IndexDataA indexDataA5 = new IndexDataA("校园公告", announCount);
        List<IndexDataA> indexDataAS = new ArrayList<>();
        indexDataAS.add(indexDataA1);
        indexDataAS.add(indexDataA2);
        indexDataAS.add(indexDataA3);
        indexDataAS.add(indexDataA4);
        indexDataAS.add(indexDataA5);

        Integer stuCount = adminMapper.selStudent();
        Integer boyStucount = adminMapper.selStudentBoy();
        double boy =Math.round ((double)boyStucount / stuCount * 100.0 *10.0) / 10.0;
        double girl = 100 - boy;
        IndexDataB indexDataB1 = new IndexDataB("男生", boy,"#0a9afe");
        IndexDataB indexDataB2 = new IndexDataB("女生", girl,"#f0657d");
        List<IndexDataB> indexDataBS = new ArrayList<>();
        indexDataBS.add(indexDataB1);
        indexDataBS.add(indexDataB2);

        Integer SHcount = adminMapper.shwCount();
        Integer completeCount = adminMapper.hwComCount();
        Integer stayCount = adminMapper.hwStayCount();
        double com = (double) Math.round((double) completeCount / SHcount * 100) /100 ;
        System.out.println(com);
        double stay = (double) Math.round((double) stayCount / SHcount * 100) /100 ;
        System.out.println(stay);
        IndexDataC indexDataC1 = new IndexDataC("已完成作业",completeCount,com);
        IndexDataC indexDataC2 = new IndexDataC("待提交作业",stayCount,stay);
        List<IndexDataC> indexDataCS = new ArrayList<>();
        indexDataCS.add(indexDataC1);
        indexDataCS.add(indexDataC2);

        IndexDataAll indexDataAll = new IndexDataAll(indexDataAS,indexDataBS,indexDataCS);
        return indexDataAll;
    }


}
